package org.hsieh.tr.explore.util;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.hsieh.tr.explore.common.Refreshable;
import org.hsieh.tr.explore.dbsource.impl.TableEntityImpl;

/**
 * Utils for JFace
 * 
 *@author hsieh 234aini@gmail.com
 * 
 */
public abstract class JFaceUtil {

    public static Object getFristSelected(ISelection selection) {
	if (selection == null || selection.isEmpty())
	    return null;
	if (selection instanceof IStructuredSelection) {
	    IStructuredSelection ss = (IStructuredSelection) selection;
	    return ss.getFirstElement();
	}
	return null;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getFristSelected(ISelection selection, Class<T> type) {
	if (selection == null || selection.isEmpty())
	    return null;
	if (selection instanceof IStructuredSelection) {
	    IStructuredSelection ss = (IStructuredSelection) selection;
	    Object firstElement = ss.getFirstElement();
	    if (firstElement != null && type.isInstance(firstElement)) {
		return (T) firstElement;
	    }
	}
	return null;
    }

    public static void main(String[] args) {
	TableEntityImpl tmpl = new TableEntityImpl();
	System.out.println(Refreshable.class.isInstance(tmpl));
    }
}
